#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 5e4 + 5;

int n, m, tot;
string name[N];
map<string, int> id;
int f[N];
void init(int n) { rep(i, 1, n) f[i] = i; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return;
  f[fy] = fx;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init(5e4);
  char c;
  string s;
  int x, y, z;
  while (cin >> c && c != '$') {
    cin >> s;
    if (!id.count(s)) {
      id[s] = ++tot;
      name[tot] = s;
    }
    if (c == '#')
      x = id[s];
    else if (c == '+')
      y = id[s], merge(x, y);
    else {
      z = find(id[s]);
      cout << s << ' ' << name[z] << endl;
    }
  }
  return 0;
}